package com.sunriz.javafx.server.utils.Scada;

import com.sunriz.javafx.server.Constant.ScadaConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.File;
import java.io.IOException;

/**
 * ClassName: ScadaFileUtil
 * Package: com.sunriz.javafx.server.utils
 * Description:
 *
 * @Author BuTang
 * @Create 2024/12/26 14:50
 * @Version 1.0
 */
public class FileManager {
    public static String addFile(String fileName){
        String filePath = ScadaConstant.FILE_PATH;
        File file = new File(filePath + fileName);
        try {
            // 创建文件，如果文件已存在则不会抛出异常
            boolean isCreated = file.createNewFile();
            if (isCreated) {
                return ScadaConstant.FILE_ADD_SUCCESS;
            } else {
                return ScadaConstant.FILE_ALREADY_EXIST;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return ScadaConstant.FILE_ADD_FAIL;
        }
    }

    public static String deleteFile(String fileName) {
        String filePath = ScadaConstant.FILE_PATH;
        File file = new File(filePath + fileName);

        // 检查文件是否存在
        if (file.exists()) {
            try {
                // 删除文件
                if (file.delete()) {
                    return ScadaConstant.FILE_DELETE_SUCCESS;
                } else {
                    return ScadaConstant.FILE_DELETE_FAIL;
                }
            } catch (SecurityException e) {
                e.printStackTrace();
                return ScadaConstant.FILE_DELETE_FAIL_PERMISSION;
            }
        } else {
            return ScadaConstant.FILE_NOT_EXIST;
        }
    }
}
